package com.zh.jbet.core.tournament.scorer;

import com.zh.jbet.core.scorer.GameScorer;
import com.zh.jbet.core.tournament.entity.TeamMatch;
import com.zh.jbet.core.tournament.entity.MatchResult;
import com.zh.jbet.core.tournament.entity.TeamsBet;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement(name = "teamScorer")
public class TeamBetScorer implements GameScorer<TeamMatch, TeamsBet>{

    private int drawPoint;
    private int winPoint;
    private int playPoint;

    @Override
    public void reset() {
        drawPoint = 0;
        winPoint = 0;
        playPoint = 0;
    }

    @XmlAttribute
    public int getDrawPoint() {
        return drawPoint;
    }

    public void setDrawPoint(int drawPoint) {
        this.drawPoint = drawPoint;
    }

    @XmlAttribute
    public int getWinPoint() {
        return winPoint;
    }

    public void setWinPoint(int winPoint) {
        this.winPoint = winPoint;
    }

    @XmlAttribute
    public int getPlayPoint() {
        return playPoint;
    }

    public void setPlayPoint(int playPoint) {
        this.playPoint = playPoint;
    }

    @XmlTransient
    @Override
    public Class<TeamsBet> getBetClass() {
        return TeamsBet.class;
    }

    @Override
    public int score(TeamMatch match, TeamsBet bet) {
        int ret = 0;
        boolean ishome = bet.getTeams().contains(match.getHome());
        boolean isaway = bet.getTeams().contains(match.getAway());
        if (ishome || isaway) {
            ret += ishome && isaway ? 2 * playPoint : playPoint;
            MatchResult res = match.getResult();
            if (res.isAwayWin()) {
                if (isaway)
                    ret += winPoint;
            } else if (res.isHomeWin()) {
                if (ishome)
                    ret += winPoint;
            } else {
                ret += drawPoint;
                if (ishome && isaway) {
                    ret += drawPoint;
                }
            }
        }
        return ret;
    }

}
